package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FeatureController")
public class FeatureController extends CmiEmaController {

	@RequestMapping("/listFeatures")
	public ModelAndView listFeatures(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = featurePaginationService.updatePagination(request, pagination);
		Set<Feature> features = featurePaginationService.getPaginationFeatures(pagination);

		mav.addObject("features", features);
		mav.addObject("entityName", "feature");
		mav.addObject("pagination", pagination);
		mav.setViewName("feature/listFeatures.jsp");
		return mav;
	}

	public String listFeatures() {
		return "redirect:/listFeatures";
	}

	@RequestMapping("/viewFeature")
	public ModelAndView viewFeature(@RequestParam Integer featureId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("feature", featureDAO.findFeatureByPrimaryKey(featureId));
		mav.setViewName("feature/viewFeature.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFeatures")
	public ModelAndView confirmDeleteFeatures(
			@RequestParam(value = "deleteFeaturesIds", required = true) Set<Integer> deleteFeaturesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Feature> deleteFeatures = featureDAO.findFeaturesByIds(deleteFeaturesIds);
		Set<Feature> referencedFeatures = featureService.loadReferenceFeatures(deleteFeatures);
		boolean hasReferenced;
		if (referencedFeatures.size() == 0) {
			mav.addObject("deleteEntitys", deleteFeatures);
			hasReferenced = false;
		} else {
			deleteFeatures.removeAll(referencedFeatures);
			mav.addObject("referencedEntitys", referencedFeatures);
			mav.addObject("deleteEntitys", deleteFeatures);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Feature");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFeatures");
		return mav;
	}

	@RequestMapping("/deleteFeature")
	public String deleteFeature(@RequestParam Integer featureId) {
		Feature feature = featureDAO.findFeatureByPrimaryKey(featureId);
		featureService.deleteFeature(feature);
		return "forward:/listFeatures";
	}

	@RequestMapping("/confirmDeleteFeature")
	public ModelAndView confirmDeleteFeature(@RequestParam Integer featureId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("feature", featureDAO.findFeatureByPrimaryKey(featureId));

		mav.setViewName("feature/deleteFeature.jsp");

		return mav;
	}

	@RequestMapping("/deleteFeatures")
	public String deleteFeatures(
			@RequestParam(value = "deleteFeaturesId", required = true) Set<Integer> deleteFeaturesId) {
		featureService.deleteFeatures(deleteFeaturesId);
		return "forward:/listFeatures";
	}

	@RequestMapping("/editFeature")
	public ModelAndView editFeature(@RequestParam Integer featureId) {

		ModelAndView mav = new ModelAndView();

		Feature feature = featureDAO.findFeatureByPrimaryKey(featureId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("feature", feature);

		// 把所有的引用feature对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(featureService.loadFeatureObjectReferencedSet(feature));

		// 将编辑页面feature添加到mav中
		mav.setViewName("feature/editFeature.jsp");
		return mav;
	}

	@RequestMapping("/newFeature")
	public ModelAndView newFeature() {
		ModelAndView mav = new ModelAndView();
		Feature feature = new Feature();

		mav.addObject("feature", feature);

		// 把所有的引用feature对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(featureService.loadFeatureObjectReferencedSet(feature));

		mav.addObject("newFlag", true);
		mav.setViewName("feature/editFeature.jsp");

		return mav;
	}

	@RequestMapping("/saveFeature")
	public ModelAndView saveFeature(@ModelAttribute Feature feature) {

		ModelAndView mav = new ModelAndView();

		Detectmethod detectmethodTemp = feature.getDetectmethod();
		Detectmethod detectmethod = detectmethodTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(detectmethodDAO.findDetectmethodById(detectmethodTemp.getId()));

		Feature existingFeature = CmiHibernateProxyUtil
				.deproxy(featureDAO.findFeatureByPhysicalPrimaryKey(feature.getName(), detectmethod.getId()));

		feature.setDetectmethod(detectmethod);
		// 新建记录
		if (feature.getId() == null) {
			if (feature.equals(existingFeature)) {

				mav.addAllObjects(featureService.loadFeatureClassReferencedSet(feature));
				// mav = editContinue(mav, feature);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("feature/editFeature.jsp");
			} else {
				featureService.saveFeature(feature);
				mav.setViewName("forward:/listFeatures");
			}
		}
		// 编辑记录
		else {
			Feature originFeature = CmiHibernateProxyUtil.deproxy(featureDAO.findFeatureByPrimaryKey(feature.getId()));
			if (!feature.equals(originFeature) && feature.equals(existingFeature)) {
				mav.addAllObjects(featureService.loadFeatureClassReferencedSet(feature));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("feature/editFeature.jsp");
			} else {
				featureService.saveFeature(feature);
				mav.setViewName("forward:/listFeatures");
			}
		}
		return mav;
	}

	@RequestMapping("/featureController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
